<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto">
      <el-form-item prop="scCode">
        <el-input v-model.trim="queryParams.scCode" placeholder="请输入编码" clearable style="width: 130px" />
      </el-form-item>
      <el-form-item prop="scName">
        <el-input v-model.trim="queryParams.scName" placeholder="请输入名称" clearable style="width: 130px" />
      </el-form-item>
      <el-form-item prop="specialTypeId">
        <el-select v-model="queryParams.specialTypeId" placeholder="专业类别" clearable style="width: 130px">
          <el-option v-for="dict in specialTypes" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="scType">
        <el-select v-model="queryParams.scType" placeholder="类型" clearable style="width: 130px">
          <el-option v-for="dict in cqCheckScTypes" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" :icon="isExpand ? 'el-icon-arrow-down' : 'el-icon-arrow-right'" @click="handleExpandDebounce">全部{{ isExpand ? '折叠' : '展开' }}</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="$refs.EditCheckSc.open({}, { treeData: list })" v-hasPermi="['cq:sc:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table class="g-table-tree-expand-align" ref="table" v-loading="loading" :data="filteredList" size="small" row-key="scId" :default-expand-all="isExpand" height="calc(100vh - 150px)">
      <el-table-column label="编码" prop="scCode" show-overflow-tooltip min-width="150" class-name="expand-column" />
      <el-table-column label="名称" prop="scName" show-overflow-tooltip min-width="200" />
      <el-table-column label="专业类别" align="center" min-width="70">
        <dict-tag slot-scope="{ row }" :options="specialTypes" :value="row.specialTypeId" />
      </el-table-column>
      <el-table-column label="类型" prop="scType" align="center" width="60">
        <span slot-scope="{ row }">{{ row.scType | scTypeFilter }}</span>
      </el-table-column>
      <el-table-column label="是否启用" align="center" min-width="80">
        <el-tag slot-scope="{ row }" size="small" :type="row.isEnable ? 'primary' : 'info'">{{ row.isEnable ? '是' : '否' }}</el-tag>
      </el-table-column>
      <el-table-column label="排序" prop="seq" align="center" width="50" />
      <el-table-column label="操作" fixed="right" align="center" width="180">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditCheckSc.open(row, { treeData: list })" v-hasPermi="['cq:sc:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-hasPermi="['cq:sc:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="$refs.EditCheckSc.open({ parentId: row.scId }, { treeData: list })" v-hasPermi="['cq:sc:add']">新增</el-button>
        </template>
      </el-table-column>
    </el-table>

    <EditCheckSc ref="EditCheckSc" v-model="showEditCheckSc" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-安全文明页面
import { debounce, flatList2tree, isLikeOrEqual } from '@/utils'
import { cqCheckScTypes } from '@/utils/baseData'
import { getSpecialTypes } from '@/utils/dict/custom'
import { listCheckSc, delCheckSc } from '@/api/qualityManage/cq'
import EditCheckSc from './components/EditCheckSc'
export default {
  name: 'Sc',
  components: { EditCheckSc },
  filters: {
    scTypeFilter(val) {
      const item = cqCheckScTypes.find(i => i.value === val)
      return item?.label || val
    }
  },
  data() {
    return {
      cqCheckScTypes,
      isExpand: true,
      loading: false,
      list: [],
      filteredList: [],
      queryParams: {
        scCode: '',
        scName: '',
        specialTypeId: '',
        scType: ''
      },
      showEditCheckSc: false,
      specialTypes: []
    }
  },
  watch: {
    queryParams: {
      deep: true,
      handler() {
        this.handleQueryDebounce()
      }
    }
  },
  created() {
    this.handleQueryDebounce = debounce(this.handleQuery)
    this.handleExpandDebounce = debounce(this.handleExpand)
    getSpecialTypes().then(res => (this.specialTypes = res))
    this.getList()
  },
  methods: {
    // 递归切换展开状态
    setChildren(children, expanded) {
      children.map(row => {
        this.$refs.table.toggleRowExpansion(row, expanded)
        if (row.children) this.setChildren(row.children, expanded)
      })
    },
    // 展开/折叠
    handleExpand() {
      this.isExpand = !this.isExpand
      this.setChildren(this.filteredList, this.isExpand)
    },
    async getList() {
      this.loading = true
      // const res = await listCheckSc(this.queryParams).catch(() => false)
      const res = await listCheckSc().catch(() => false)
      this.loading = false
      if (res === false) return

      let data = res.data || []
      data.map(i => {
        i.removeLoading = false
      })
      data = flatList2tree(data, { id: 'scId' })
      this.list = data
      this.handleQuery()
    },
    handleQuery() {
      // 本地树形搜索
      const { scCode, scName, specialTypeId, scType } = this.queryParams
      const opts = { like: { scCode, scName }, eq: { specialTypeId, scType } }
      const list = JSON.parse(JSON.stringify(this.list))
      const recursion = list =>
        list.filter(item => {
          if (item.children?.length) item.children = recursion(item.children)
          return isLikeOrEqual(item, opts) || item.children?.length
        })
      const data = recursion(list)
      this.filteredList = data
      this.isExpand = true
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.getList()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm('确认删除名称为"' + item.scName + '"的数据项?', '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delCheckSc(item.scId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.getList()
    }
  }
}
</script>
